﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_Product_ProductImages : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        Ajax.Utility.RegisterTypeForAjax(typeof(Manager_Product_ProductImages));
        if (!IsPostBack)
        {
            BindData();
        }
        else
        {
            if (hMethod.Value == "Save_Click")
            {
                Save_Click();
                hMethod.Value = "";
                hFile.Value = "";
                hLink.Value = "";
            }
        }
        Session["dtlDetailFile"] = dtlDetailFile;
    }
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string AjaxDelFile(string ImgPD_ID, string name, string PDID)
    {
        clsWebShop cls = new clsWebShop();
        cls.UpdateData("delete Product_IMG where ImgPD_ID=" + ImgPD_ID + "");
        string path = Server.MapPath("~/") + name;
        try
        {
            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }
        }
        catch { };
        DataTable dtD = new DataTable();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("PDID", PDID);
        dtD = cls.Get_Product_IMGByCondition(msg.GetXmlStr).Tables[0];

        if (dtD.Rows.Count > 0)
        {
            DataList dtlDetail = (DataList)Session["dtlDetailFile"];
            dtlDetail.DataSource = dtD;
            dtlDetail.DataBind();
            return GetControlRenderString(dtlDetail);
        }
        return "";
    }
    private void Save_Click()
    {

        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
       
        string sFile = hFile.Value;
        if (sFile.Trim() != "" || hLink.Value != "")
        {

            sFile = sFile.Substring(sFile.LastIndexOf(@"\") + 1);
            string sName = hFileName.Value.Substring(hFileName.Value.LastIndexOf(@"\") + 1);
            string[] arrFile = sFile.Split('＊');
            string[] arrName = hFileName.Value.Split('＊');
            string[] arrLink = hLink.Value.Split('＊');
            
            for (int i = 0; i < arrFile.Length; i++)
            {
                string link = arrLink[i].ToString();
                if (link.Length <= 0 || link.Trim() == "http://")
                {
                    link = "Images/ProductIMG/" + arrFile[i].ToString();
                    if (Request.Files[i].ContentLength > 0)
                    {
                        Request.Files[i].SaveAs(Server.MapPath("~") + "/" + link);
                    }
                }

                PccMsg msgs = new PccMsg();
                msgs.CreateFirstNode("ImgPD_ID", "0");
                msgs.CreateFirstNode("PDID", Request.QueryString["PDID"]);
                msgs.CreateFirstNode("ImgPD_Desc", arrName[i].ToString());
                msgs.CreateFirstNode("ImgPD_Url", link);
                msgs.CreateFirstNode("UpdID", Session["AdminUserID"].ToString());
                string strReturn = cls.Ins_Upd_Product_IMG(msgs.GetXmlStr.ToString());

            }
        }


        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Bảo lưu thành công'); window.location.href='ProductImages.aspx?KindNo=" + Request.QueryString["KindNo"] + "&CodeID=" + Request.QueryString["CodeID"] + "&sTopicID=" + Request.QueryString["sTopicID"] + "&sCatID=" + Request.QueryString["sCatID"] + "&PDID=" + Request.QueryString["PDID"] + "' </script>");



    }
    string GetControlRenderString(System.Web.UI.Control ctrl)
    {
        string ret = "";
        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        try
        {
            ctrl.RenderControl(htmlWriter);
            ret = stringWriter.ToString().Replace("\n", "").Replace("\t", "").Replace("\r", "");
        }
        catch (Exception e)
        {
            ret = e.Message;
        }
        return ret;
    }
    private void BindData()
    {
        DataTable dt = new DataTable();
        if (Request.QueryString["PDID"] != "" && Request.QueryString["PDID"] != null)
        {
            clsWebShop cls = new clsWebShop();
            PccMsg msg = new PccMsg();
            msg.CreateFirstNode("PDID", Request.QueryString["PDID"]);
            dt = cls.Get_Product_IMGByCondition(msg.GetXmlStr).Tables[0];
           
            dtlDetailFile.DataSource = dt;
            dtlDetailFile.DataBind();
            
        }
      
        //DataColumn col=new DataColumn("")
        dt = new DataTable();
        DataRow dr = dt.NewRow();
        dt.Rows.Add(dr);
        
        dtlFile.DataSource = dt;
        dtlFile.DataBind();
    }
    private void ReturnPage()
    {
        Response.Redirect("Product.aspx?KindNo=" + Request.QueryString["KindNo"] + "&CodeID=" + Request.QueryString["CodeID"] + "&sTopicID=" + Request.QueryString["sTopicID"] + "&sCatID=" + Request.QueryString["sCatID"] + "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ReturnPage();
    }

    protected void dtlDetailFile_ItemDataBound(object sender, DataListItemEventArgs e)
    {

    }
    protected void dtlDetailFile_ItemDataBound1(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink hplLink = (HyperLink)e.Item.FindControl("hplLink");
            if (hplLink.NavigateUrl.IndexOf("http://") == -1)//Khong tim thay
            {
                hplLink.NavigateUrl = ResolveUrl("~/") + hplLink.NavigateUrl;
            }
        }
    }
}
